package com.yoshino.singleton.type7;

import lombok.extern.slf4j.Slf4j;

/**
 * @author yoshino
 **/
@Slf4j
public class SingletonTest07 {
    public static void main(String[] args) {
        // 测试
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        // true
        log.info(String.valueOf(instance == instance2));
        log.info(instance.hashCode() + "");
        log.info(instance2.hashCode() + "");
    }
}

// 静态内部类

@Slf4j
class Singleton {

    // 1.构造器私有化，外部不能 new

    private Singleton() {
    }

    // 写静态内部类，该类中有一个静态属性 Singleton

    private static class SingletonInstance {
        private static final Singleton INSTANCE = new Singleton();
    }
    // 提供一个静态的公有方法,直接返回 SingletonInstance.INSTANCE

    public static Singleton getInstance() {
        return SingletonInstance.INSTANCE;
    }



}